// components/short-video-goods-view/index.ts
const hideScrollBar = require("../../behavior/hide-scroll-bar")
import { getProduct, getADV } from "../../api/index"
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [hideScrollBar],
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    shopAnimate: "" as any,
    productList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async tapShop(e) {
      const GoodsType = e.detail.GoodsType;
      const ADVId = e.detail.ADVId;
      const SelfGoodsIds = e.detail.SelfGoodsIds;
      const animation = wx.createAnimation({ duration: 250 });
      console.log("GoodsType", GoodsType);

      // 自营商品
      if (GoodsType == 1 && SelfGoodsIds.length > 0) {
        animation.height("62vh").step();
        this.setData({
          shopAnimate: animation
        });
        this.getGoods(SelfGoodsIds)
        return
      }
      // 自营商品
      //------ 第三方
      let adValue = ""
      if (GoodsType == 2 && ADVId) adValue = await getADV({ "Id": ADVId }).then(res => res.Data.ADVUrl);
      if (adValue.includes("小程序")) {
        wx.navigateToMiniProgram({
          appId: '',
          shortLink: adValue
        })
      } else {
        wx.navigateTo({
          url: `/pages/play-page/views/adview/index?adlink=${adValue}`
        })
      }
      //------ 第三方
    },
    async getGoods(ids) {
      let productList: any = []
      for (let index = 0; index < ids.length; index++) {
        const id = ids[index];
        const product: any = await getProduct({ ProductId: Number(id), OutProductId: "", NeedEditSpu: 0 }).then((res: any) => res.Data.Product);
        productList.push(product)
      }
      productList.forEach(element => {
        let min_price = String(element.min_price);
        element["price"] = `${min_price.slice(0, min_price.length - 2)}.${min_price.slice(min_price.length - 2, min_price.length)}`
      });
      this.setData({ productList })
    },
    toShop(event) {
      const { product_id } = event.currentTarget.dataset;
      wx.navigateTo({
        url: `plugin-private://wx34345ae5855f892d/pages/productDetail/productDetail?productId=${product_id}`,
      });
    },
    tapCloseMask() {
      const animation = wx.createAnimation({ duration: 250 });
      animation.height(0).step()
      this.setData({
        shopAnimate: animation
      })
    },
  }
})
